package org.jeecg.modules.demo.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.crm.entity.CPaymentRequestApply;
import org.jeecg.modules.demo.crm.entity.CReimbursement;
import org.jeecg.modules.demo.crm.mapper.CPaymentRequestApplyMapper;
import org.jeecg.modules.demo.crm.service.ICCommonService;
import org.jeecg.modules.demo.crm.service.ICPaymentRequestApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Description: 付款申请
 * @Author: jeecg-boot
 * @Date:   2025-06-20
 * @Version: V1.0
 */
@Service
public class CPaymentRequestApplyServiceImpl extends ServiceImpl<CPaymentRequestApplyMapper, CPaymentRequestApply> implements ICPaymentRequestApplyService {

    @Autowired
    CPaymentRequestApplyMapper cPaymentRequestApplyMapper;

    @Autowired
    private ICCommonService cCommonService;

    @Override
    public void audit(CPaymentRequestApply cPaymentRequestApply) {
        CPaymentRequestApply aply = cPaymentRequestApplyMapper.selectById(cPaymentRequestApply.getId());
        String status = cPaymentRequestApply.getStatus();
        if("同意".equals(status)){
            aply.setStatus("待付款");
        }else {
            aply.setStatus("驳回");
        }
        aply.setReviewDescription(cPaymentRequestApply.getReviewDescription());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        aply.setReviewerUserId(sysUser.getId());
        aply.setReviewTime(new Date());
        cPaymentRequestApplyMapper.updateById(aply);
    }

    @Override
    public Map<String, Long> statusCount(CPaymentRequestApply cPaymentRequestApply, HttpServletRequest req) {

        CPaymentRequestApply query = new CPaymentRequestApply();
        Map<String, Long> data = new HashMap<>();
        List<String> statusList = new ArrayList<>();
        statusList.add("待审核");
        statusList.add("待付款");
        statusList.add("已付款");
        statusList.add("驳回");
        statusList.add("全部");
        QueryWrapper<CPaymentRequestApply> queryWrapper = null;
        Boolean permissionCheck = cCommonService.taskpermissionCheck();
        for (String status : statusList) {
            queryWrapper = QueryGenerator.initQueryWrapper(query, req.getParameterMap());
            if(!status.equals("全部") ){
                queryWrapper.eq("status", status);
            }
            if(cPaymentRequestApply != null && cPaymentRequestApply.getProjectId() != null){
                queryWrapper.eq("project_id",cPaymentRequestApply.getProjectId());
            }
            if(cPaymentRequestApply != null && cPaymentRequestApply.getSubProjectId()!= null){
                queryWrapper.eq("sub_project_id",cPaymentRequestApply.getSubProjectId());
            }

            if(permissionCheck){
                LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
                queryWrapper.eq("submitter_user_id",sysUser.getId());
            }
            data.put(status,cPaymentRequestApplyMapper.selectCount(queryWrapper));
        }
        return data;
    }
}
